Load all required libraries.

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.3
## -- Attaching packages ------------------------------------------------------------------------ tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.0
## v tidyr   1.1.0     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## Warning: package 'ggplot2' was built under R version 3.6.3
## Warning: package 'tibble' was built under R version 3.6.3
## Warning: package 'readr' was built under R version 3.6.3
## Warning: package 'dplyr' was built under R version 3.6.3
## Warning: package 'forcats' was built under R version 3.6.3
## -- Conflicts --------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## Warning: package 'plotly' was built under R version 3.6.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)
## Warning: package 'broom' was built under R version 3.6.3

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: Ignoring 1 observations
        p2
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "cases_cum_clarke", "new_cases_clarke", "X7_day_ave_clarke", "cases_per_100000_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "cases_cum_clarke", "new_cases_clarke", "X7_day_ave_clarke", "cases_per_100000_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "cases_cum_clarke", "new_cases_clarke", "X7_day_ave_clarke", "cases_per_100000_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 247)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 12.81030 12.81862 12.82683 12.83492 12.84287 12.85070 12.85838 12.86592
##   [9] 12.87331 12.88054 12.88762 12.89453 12.90126 12.90783 12.91421 12.92040
##  [17] 12.92640 12.93220 12.93780 12.94319 12.94837 12.95333 12.95806 12.96255
##  [25] 12.96683 12.97088 12.97473 12.97838 12.98184 12.98512 12.98823 12.99117
##  [33] 12.99395 12.99658 12.99907 13.00143 13.00367 13.00579 13.00780 13.00971
##  [41] 13.01154 13.01328 13.01494 13.01648 13.01782 13.01897 13.01992 13.02070
##  [49] 13.02129 13.02169 13.02192 13.02198 13.02186 13.02157 13.02111 13.02048
##  [57] 13.01970 13.01875 13.01765 13.01640 13.01499 13.01343 13.01173 13.00988
##  [65] 13.00789 13.00577 13.00351 13.00111 12.99859 12.99594 12.99316 12.99026
##  [73] 12.98724 12.98410 12.98086 12.97749 12.97402 12.97045 12.96677 12.96299
##  [81] 12.95911 12.95514 12.95107 12.94691 12.94267 12.93799 12.93257 12.92645
##  [89] 12.91968 12.91229 12.90435 12.89590 12.88698 12.87764 12.86792 12.85788
##  [97] 12.84756 12.83701 12.82627 12.81539 12.80442 12.79340 12.78238 12.77141
## [105] 12.76053 12.74979 12.73924 12.72892 12.71888 12.70917 12.69984 12.69092
## [113] 12.68247 12.67454 12.66716 12.66039 12.65428 12.64886 12.64420 12.64032
## [121] 12.63729 12.63514 12.63393 12.63370 12.63449 12.63636 12.63935 12.64351
## [129] 12.64866 12.65458 12.66120 12.66848 12.67636 12.68478 12.69369 12.70304
## [137] 12.71277 12.72282 12.73315 12.74370 12.75441 12.76523 12.77611 12.78699
## [145] 12.79781 12.80853 12.81908 12.82942 12.83949 12.84974 12.86063 12.87211
## [153] 12.88410 12.89657 12.90944 12.92267 12.93619 12.94994 12.96388 12.97793
## [161] 12.99204 13.00616 13.02023 13.03743 13.06023 13.08752 13.11817 13.15104
## [169] 13.18501 13.21895 13.25174 13.28225 13.30935 13.33192 13.34883 13.36249
## [177] 13.37605 13.38943 13.40258 13.41544 13.42796 13.44006 13.45170 13.46280
## [185] 13.47332 13.48319 13.49236 13.50076 13.50833 13.51501 13.52075 13.52549
## [193] 13.52916 13.53171 13.53307 13.53319 13.53200 13.52945 13.52548 13.52003
## [201] 13.51304 13.50444 13.49419 13.48221 13.46845 13.45320 13.43679 13.41927
## [209] 13.40066 13.38101 13.36035 13.33872 13.31615 13.29268 13.26835 13.24318
## [217] 13.21722 13.19051 13.16307 13.13494 13.10617 13.07678 13.04681 13.01630
## [225] 12.98528 12.95379 12.92171 12.88889 12.85533 12.82104 12.78600 12.75022
## [233] 12.71370 12.67644 12.63843 12.59967 12.56016 12.51990 12.47890 12.43714
## [241] 12.39462 12.35135 12.30733 12.26255 12.21700 12.17070 12.12364
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.6, n = 247)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.51715 12.51760 12.51801 12.51840 12.51877 12.51913 12.51951 12.51990
##   [9] 12.52033 12.52080 12.52132 12.52192 12.52259 12.52335 12.52421 12.52519
##  [17] 12.52629 12.52753 12.52892 12.53048 12.53220 12.53411 12.53619 12.53842
##  [25] 12.54077 12.54323 12.54579 12.54843 12.55115 12.55391 12.55671 12.55954
##  [33] 12.56238 12.56521 12.56802 12.57080 12.57353 12.57619 12.57878 12.58127
##  [41] 12.58366 12.58592 12.58804 12.59025 12.59277 12.59558 12.59865 12.60199
##  [49] 12.60558 12.60939 12.61341 12.61764 12.62204 12.62662 12.63134 12.63620
##  [57] 12.64119 12.64628 12.65146 12.65671 12.66203 12.66739 12.67278 12.67818
##  [65] 12.68358 12.68897 12.69432 12.69963 12.70487 12.71004 12.71511 12.72008
##  [73] 12.72492 12.72962 12.73417 12.73854 12.74274 12.74673 12.75050 12.75405
##  [81] 12.75735 12.76038 12.76314 12.76561 12.76777 12.76836 12.76629 12.76173
##  [89] 12.75489 12.74596 12.73514 12.72263 12.70862 12.69330 12.67687 12.65953
##  [97] 12.64148 12.62290 12.60400 12.58497 12.56601 12.54731 12.52907 12.51148
## [105] 12.49475 12.47906 12.46461 12.45159 12.44022 12.43067 12.42314 12.41784
## [113] 12.41495 12.41343 12.41213 12.41110 12.41040 12.41009 12.41022 12.41086
## [121] 12.41204 12.41384 12.41630 12.41949 12.42346 12.42826 12.43395 12.44059
## [129] 12.44910 12.46022 12.47372 12.48940 12.50702 12.52637 12.54722 12.56936
## [137] 12.59257 12.61662 12.64129 12.66638 12.69164 12.71687 12.74184 12.76634
## [145] 12.79014 12.81302 12.83476 12.85515 12.87396 12.89291 12.91367 12.93591
## [153] 12.95933 12.98359 13.00838 13.03338 13.05827 13.08273 13.10644 13.12909
## [161] 13.15035 13.16991 13.18744 13.20651 13.23023 13.25759 13.28755 13.31911
## [169] 13.35124 13.38293 13.41315 13.44089 13.46513 13.48485 13.49904 13.50958
## [177] 13.51906 13.52749 13.53486 13.54117 13.54644 13.55066 13.55383 13.55597
## [185] 13.55707 13.55714 13.55617 13.55418 13.55116 13.54712 13.54206 13.53599
## [193] 13.52890 13.52080 13.51170 13.50159 13.49048 13.47838 13.46528 13.45119
## [201] 13.43611 13.42004 13.40300 13.38497 13.36597 13.34597 13.32497 13.30295
## [209] 13.27993 13.25592 13.23091 13.20492 13.17793 13.14997 13.12104 13.09113
## [217] 13.06026 13.02842 12.99563 12.96189 12.92719 12.89156 12.85498 12.81747
## [225] 12.77903 12.73966 12.69936 12.65811 12.61592 12.57277 12.52868 12.48364
## [233] 12.43765 12.39071 12.34282 12.29398 12.24418 12.19343 12.14173 12.08908
## [241] 12.03547 11.98090 11.92538 11.86890 11.81147 11.75308 11.69373
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.6, n = 247)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.47144 11.49681 11.52182 11.54644 11.57069 11.59453 11.61797 11.64098
##   [9] 11.66357 11.68572 11.70741 11.72865 11.74941 11.76968 11.78946 11.80874
##  [17] 11.82750 11.84573 11.86342 11.88056 11.89715 11.91316 11.92859 11.94343
##  [25] 11.95766 11.97128 11.98427 11.99663 12.00834 12.01933 12.02957 12.03908
##  [33] 12.04790 12.05606 12.06360 12.07056 12.07696 12.08284 12.08823 12.09317
##  [41] 12.09770 12.10184 12.10563 12.10911 12.11230 12.11525 12.11798 12.12054
##  [49] 12.12295 12.12524 12.12677 12.12687 12.12561 12.12305 12.11925 12.11426
##  [57] 12.10815 12.10098 12.09280 12.08368 12.07368 12.06285 12.05125 12.03894
##  [65] 12.02599 12.01245 11.99838 11.98385 11.96890 11.95361 11.93802 11.92220
##  [73] 11.90622 11.89012 11.87397 11.85782 11.84174 11.82579 11.81003 11.79451
##  [81] 11.77929 11.76444 11.75001 11.73607 11.72267 11.70987 11.69773 11.68632
##  [89] 11.67568 11.66589 11.65699 11.64906 11.64139 11.63330 11.62484 11.61610
##  [97] 11.60711 11.59796 11.58870 11.57940 11.57011 11.56091 11.55185 11.54301
## [105] 11.53443 11.52619 11.51835 11.51097 11.50412 11.49786 11.49224 11.48735
## [113] 11.48323 11.47995 11.47757 11.47617 11.47580 11.47652 11.47839 11.48149
## [121] 11.48588 11.49161 11.49875 11.50736 11.51752 11.52927 11.54269 11.55783
## [129] 11.57582 11.59752 11.62257 11.65066 11.68143 11.71455 11.74968 11.78648
## [137] 11.82461 11.86374 11.90353 11.94363 11.98370 12.02342 12.06244 12.10042
## [145] 12.13703 12.17192 12.20476 12.23520 12.26292 12.29002 12.31865 12.34849
## [153] 12.37922 12.41055 12.44215 12.47371 12.50492 12.53548 12.56506 12.59335
## [161] 12.62006 12.64485 12.66742 12.69001 12.71470 12.74097 12.76828 12.79611
## [169] 12.82391 12.85117 12.87734 12.90190 12.92432 12.94406 12.96060 12.97508
## [177] 12.98898 13.00227 13.01493 13.02693 13.03823 13.04882 13.05866 13.06774
## [185] 13.07601 13.08345 13.09004 13.09574 13.10054 13.10439 13.10728 13.10918
## [193] 13.11005 13.10988 13.10863 13.10627 13.10279 13.09814 13.09231 13.08526
## [201] 13.07697 13.06741 13.05655 13.04437 13.03083 13.01610 13.00036 12.98359
## [209] 12.96578 12.94692 12.92699 12.90598 12.88388 12.86069 12.83637 12.81093
## [217] 12.78435 12.75662 12.72772 12.69765 12.66639 12.63392 12.60025 12.56535
## [225] 12.52921 12.49181 12.45322 12.41346 12.37256 12.33050 12.28728 12.24292
## [233] 12.19740 12.15073 12.10291 12.05394 12.00383 11.95256 11.90015 11.84658
## [241] 11.79187 11.73602 11.67902 11.62087 11.56158 11.50115 11.43957
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")